package com.core.sgip.listener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.core.sgip.server.SGIPReceiveServer;

@Component("processor")
public class SGIPServiceServlet extends HttpServlet {

	private static final long serialVersionUID = -9045451275234606838L;

	private static final Logger logger = Logger.getLogger(SGIPServiceServlet.class);

	// Servlet的init方法会在Tomcat启动的时候执行
	@Override
	public void init() throws ServletException {
		FutureTask<String> task = new FutureTask<String>(new Callable<String>() {

			@Override
			public String call() throws Exception {
				start(); // 使用另一个线程来执行该方法，会避免占用Tomcat的启动时间
				return "Collection Completed";
			}

		});

		new Thread(task).start();
	}

	// 希望Tomcat启动结束后执行的方法
	private void start() {
		SGIPReceiveServer.getInstance().startSGIPService();
		logger.debug(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
				+ ": SGIPReceiveServer service 启动成功");
	}

}
